package jh.jianguan.springboot.util;

import jh.jianguan.springboot.util.common.exception.ExceptionUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @description: redis工具类
 * @author: guoc
 * @createDate: 2020/11/25 16:42
 */
@Repository
public class RedisHashUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 存储单个值至map中
     * @param redisKey redisKey中的key
     * @param mapKey  map所对应的key
     * @param value map所对应的值
     */
    public void addMapOne(String redisKey,String mapKey,Object value) {
        redisTemplate.opsForHash().put(redisKey, mapKey,value);
    }

    /**
     * 存储整个map至redis
     * @param key redis中存储的key
     * @param map 需缓存的Map
     */
    public void addMapAll(String key, Map<String,Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * 获取整个HashMap
     * @param redisKey redis中存储的key
     * @return 整个Map
     */
    public Map<String,Object> getMapAll(String redisKey) {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(redisKey);
        Map<String, Object> retEntries = new HashMap<>();
        for(Map.Entry<Object , Object> temp:entries.entrySet()){
            Object key = temp.getKey();
            Object value = temp.getValue();
            retEntries.put(String.valueOf(key) , value);
        }
        return retEntries;
    }

    /**
     * 获取redis中hash的所有value
     * @param redisKey
     * @return
     */
    public List<Object> getMapValues(String redisKey) {
        return redisTemplate.opsForHash().values(redisKey);
    }

    /**
     * 删除Map中的某个键值对
     * @param redisKey
     * @param mapKey
     * @return 返回影响数量
     */
    public Long deleteMapVal(String redisKey , Object ... mapKey) {
        return redisTemplate.opsForHash().delete(redisKey , mapKey);
    }

    /**
     * 删除指定大Key
     * @param key
     * @return
     */
    public Boolean deleteKey(String key){
        return redisTemplate.delete(key);
    }

    /**
     * 确定hashkey是否存在
     * @param redisKey redis存储的key
     * @param mapKey 需要确定的map对象key
     * @return
     */
    public boolean hasKey(String redisKey , String mapKey) {
        return	redisTemplate.opsForHash().hasKey(redisKey , mapKey);
    }
    /**
     * 获取Map中具体的值
     * @param redisKey redis存储的key
     * @param mapKey 获取的map对象key
     * @return
     */
    public Object getMapVal(String redisKey, String mapKey) {
        return redisTemplate.opsForHash().get(redisKey,mapKey);
    }

    /**
     * 从哈希中获取给定key的值
     * @param redisKey redis存储的key
     * @param mapKeys 需要去出的key的集合
     * @return 值列表
     */
    public List<Object> multiGetHash(String redisKey , List<Object> mapKeys) {
        return  redisTemplate.opsForHash().multiGet(redisKey , mapKeys);
    }

    /**
     * 获取所有map中的key
     * @param redisKey
     * @return
     */
    public Set<String> getHashKeys(String redisKey) {
        Set<Object> keys = redisTemplate.opsForHash().keys(redisKey);
        Set<String> retKeys = new HashSet<>();
        for (Object key : keys) {
            retKeys.add(String.valueOf(key));
        }
        return retKeys;
    }

    /**
     * 获取所有map中的key的数量
     * @param redisKey redis中的key
     * @return key的数量
     */
    public int getHashSize(String redisKey) {
        Set<Object> keys = redisTemplate.opsForHash().keys(redisKey);
        if(keys == null){
            return 0;
        }
        return keys.size();
    }

    /**
     * 设置缓存到期时间
     * @return boolean
     * @author guoc
     * @date 2020/11/26 10:03
     */
    public boolean expire(String key,long time){
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将list放入缓存
     * @return java.lang.Boolean
     * @author guoc
     * @date 2020/11/27 16:11
     */
    public Boolean setListVal(String key,Object value){
        try {
            redisTemplate.opsForList().rightPush(key,value);
            return true;
        }catch (Exception ex){
            throw ExceptionUtil.mxe("插入List缓存失败！"+ex.getMessage());
        }
    }

    /**
     * 将Object数据放入List缓存，并设置时间
     * @return java.lang.Boolean
     * @author guoc
     * @date 2020/11/27 16:12
     */
    public Boolean setListVal(String key,Object value,long time){
        try {
            if(time > 0){
                redisTemplate.opsForList().rightPush(key,value);
                expire(key,time);
                return true;
            }
            return false;
        }catch (Exception ex){
            throw ExceptionUtil.mxe("插入List缓存失败！"+ex.getMessage());
        }
    }

    /**
     * 将list集合放入List缓存，并设置时间
     * @return java.lang.Boolean
     * @author guoc
     * @date 2020/11/27 16:14
     */
    public Boolean setListAll(String key,Object value, long time){
        try {
            if(time > 0){
                redisTemplate.opsForList().rightPushAll(key,value);
                this.expire(key,time);
                return true;
            }
            return false;
        }catch (Exception ex){
            throw ExceptionUtil.mxe("插入List缓存失败！"+ex.getMessage());
        }
    }

    /**
     * 根据索引获取缓存List中的内容
     * @return java.util.List<java.lang.Object>
     * @author guoc
     * @date 2020/11/27 16:16
     * start 索引开始
     * end 索引结束 0 到 -1代表所有值
     */
    public List<Object> getListVal(String key, long start, long end){
        try {
            return redisTemplate.opsForList().range(key,start,end);
        }catch (Exception ex){
            throw  ExceptionUtil.mxe("获取缓存List中的内容失败了！"+ex.getMessage());
        }
    }

    /**
     * 普通缓存放入
     * @return java.lang.Boolean
     * @author guoc
     * @date 2020/11/27 16:20
     */
    public Boolean setStringVal(String key,Object value){
        try {
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception ex) {
            throw ExceptionUtil.mxe("插入缓存失败！"+ex.getMessage());
        }
    }

    /**
     * 普通缓存获取
     * @return java.lang.Object
     * @author guoc
     * @date 2020/11/27 16:21
     */
    public Object getString(String key){
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置缓存存在时间
     * @return boolean
     * @author guoc
     * @date 2020/11/27 16:21
     */
    public boolean setString(String key, Object value, long time) {
        try {
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            return true;
        } catch (Exception ex) {
            throw ExceptionUtil.mxe("插入缓存失败！"+ex.getMessage());
        }
    }


}
